package com.smile.weather.presenter;

import android.content.Context;
import android.util.Log;
import android.widget.Toast;

import com.google.gson.Gson;
import com.smile.weather.utils.HttpUtil;
import com.smile.weather.bean.TodayWeatherData;
import com.smile.weather.fragment.BaseFragment;
import com.smile.weather.fragment.TodayFragment;
import com.smile.weather.interfaces.IMainView;

import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by smile on 2016/8/22.
 */
public class MainViewPresenter {
    private static final int JSON = 1;
    private static final int TODAY_HTML = 2;
    private static final int SEVEN_HTML = 3;
    private static String url_json = "http://d1.weather.com.cn/sk_2d/%s.html?_=1471793096835";
    private static String url_today_html = "http://www.weather.com.cn/weather1d/%s.shtml";
    private static String url_seven_html = "http://www.weather.com.cn/weather/%s.shtml";
    private Context mContext;
    private IMainView iMainView;
    private BaseFragment[] mFragment;
    private Gson gson;
    private String cityId = null;
    private boolean success;

    public MainViewPresenter(Context mContext) {
        this.mContext = mContext;
        this.gson = new Gson();
    }

    public void setiMainView(IMainView iMainView) {
        this.iMainView = iMainView;
    }

    public void setmFragment(BaseFragment[] mFragment) {
        this.mFragment = mFragment;
    }

    public void getCityWeather(String cityId) {

        this.cityId = cityId;
        success = true;

        Observable.create(new Observable.OnSubscribe<Result>() {
            @Override
            public void call(Subscriber<? super Result> subscriber) {
                subscriber.onNext(getJson());
                subscriber.onNext(getTodayHtml());
                subscriber.onNext(getSevenHtml());
                subscriber.onCompleted();
            }
        })
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<Result>() {
                    @Override
                    public void onCompleted() {
                        Log.i("mvp", "oc--加载完成");
                        iMainView.refreshComplete();
                        if (success) {
                            Toast.makeText(mContext, "已加载最新数据", Toast.LENGTH_SHORT).show();
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.i("mvp", "oe--获取出错" + e.getMessage());
                        iMainView.refreshComplete();
                        Toast.makeText(mContext, "加载失败", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onNext(Result result) {
                        if (result.getWhich() == JSON) {
                            dealJson(result);
                        } else if (result.getWhich() == TODAY_HTML) {
                            dealTodayHtml(result);
                        } else if (result.getWhich() == SEVEN_HTML) {
                            dealSevenHtml(result);
                        }
                    }
                });

    }

    private void dealJson(Result result) {

        if (result.getResult() != null) {
            StringBuilder builder = new StringBuilder(result.getResult());
            builder.delete(0, builder.indexOf("{"));
            Log.i("mvp", "dj--result = " + builder.toString());
            TodayWeatherData todayWeatherData = gson.fromJson(builder.toString(), TodayWeatherData.class);

            Log.i("mvp", "dj--" + todayWeatherData.toString());
            ((TodayFragment) mFragment[0]).setTodayWeather(todayWeatherData);
            iMainView.setTime(todayWeatherData.getTime() + " 实况");
        } else {
            if (success) {
                Toast.makeText(mContext, "加载失败", Toast.LENGTH_SHORT).show();
            }
            success = false;
        }
    }

    private void dealTodayHtml(Result result) {

        if (result.getResult() != null) {
            Log.i("mvp", "dth--" + result.getResult());
            mFragment[0].setHtml(result.getResult());

        } else {
            if (success) {
                Toast.makeText(mContext, "加载失败", Toast.LENGTH_SHORT).show();
            }
            success = false;
        }
    }

    private void dealSevenHtml(Result result) {

        if (result.getResult() != null) {
            Log.i("mvp", "dsh--" + result.getResult());
            mFragment[1].setHtml(result.getResult());

        } else {
            if (success) {
                Toast.makeText(mContext, "加载失败", Toast.LENGTH_SHORT).show();
            }
            success = false;
        }
    }

    class Result {
        //表示哪个请求
        private int which;
        //请求的结果
        private String result;

        public Result() {
        }

        public Result(int which, String result) {
            this.which = which;
            this.result = result;
        }

        public int getWhich() {
            return which;
        }

        public void setWhich(int which) {
            this.which = which;
        }

        public String getResult() {
            return result;
        }

        public void setResult(String result) {
            this.result = result;
        }
    }


    private Result getJson() {
        String url = String.format(url_json, new String(cityId));
        Log.i("mvp", "gj--url = " + url);
        String result = HttpUtil.sendHttpRequest(url);
        Log.i("mvp", "gj--result = " + result);

        return new Result(JSON, result);
    }

    private Result getTodayHtml() {
        String url = String.format(url_today_html, new String(cityId));
        Log.i("mvp", "gth--url = " + url);
        String result = HttpUtil.sendHttpRequest(url);
        Log.i("mvp", "gth--result = " + result);

        return new Result(TODAY_HTML, result);
    }

    private Result getSevenHtml() {
        String url = String.format(url_seven_html, new String(cityId));
        Log.i("mvp", "gsh--url = " + url);
        String result = HttpUtil.sendHttpRequest(url);
        Log.i("mvp", "gsh--result = " + result);

        return new Result(SEVEN_HTML, result);
    }


}
